package ro.ubbcluj.cs.damate.persistence.entities;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.sql.Timestamp;
import java.util.List;

import static javax.persistence.CascadeType.*;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.GenerationType.*;
import static javax.persistence.InheritanceType.*;

/**
 * @author Lóránd Fazakas (lorand)
 *         Date: 4/22/12
 *         Time: 1:12 AM
 */
@Table(name = "observed_entity", catalog = "damate")
@Entity
@Inheritance(strategy= JOINED)
public class ObservedObject {

    private Long observedEntityId;

    @Column(name = "observedEntityId", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    @GeneratedValue(strategy= IDENTITY)
    public Long getObservedEntityId() {
        return observedEntityId;
    }

    public void setObservedEntityId(Long observedEntityId) {
        this.observedEntityId = observedEntityId;
    }

    private Timestamp creationDate;

    @Column(name = "creationDate", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    @Basic
    public Timestamp getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Timestamp creationDate) {
        this.creationDate = creationDate;
    }

    private User owner;

    @ManyToOne(fetch = LAZY, cascade = ALL)
    @JoinColumn(name = "owner")
    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }

    private List<Task> tasks;

    @ManyToMany(fetch = LAZY, mappedBy = "assignedObjects", cascade = ALL)
    public List<Task> getTasks() {
        return tasks;
    }

    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
    }

    private List<Event> events;

    @OneToMany(fetch = LAZY, cascade = ALL, mappedBy = "relatedObject")
    public List<Event> getEvents() {
        return events;
    }

    public void setEvents(List<Event> events) {
        this.events = events;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ObservedObject that = (ObservedObject) o;

        if (observedEntityId != that.observedEntityId) return false;
        if (creationDate != null ? !creationDate.equals(that.creationDate) : that.creationDate != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result =  observedEntityId.intValue();
        result = 31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        return result;
    }
}
